package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.google.common.collect.Maps;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.social.interaction.facade.attention.IAttentionManageFacade;
import com.zhuanzhuan.hero.social.interaction.facade.common.IPostAuthorityFacade;
import com.zhuanzhuan.hero.social.interaction.facade.pricesheet.IPriceSheetManageFacade;
import com.zhuanzhuan.hero.social.interaction.vo.attention.AttentionRelationVO;
import com.zhuanzhuan.hero.social.interaction.vo.pricesheet.RecycleSheetScfVO;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.pojo.bo.UserAttentionBO;
import com.zhuanzhuan.hero.user.util.RpcUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName HeroSocialHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/1 21:11
 */
@Component
@Slf4j
public class HeroSocialHelper {

    @Resource
    private IAttentionManageFacade attentionManageFacade;

    @Resource
    private IPostAuthorityFacade postAuthorityFacade;

    @Resource
    private IPriceSheetManageFacade priceSheetManageFacade;
    
    /**
     * 查询用户的关注关系
     *  
     * @param fromUid
     * @param toUids
     * @return java.util.List<com.zhuanzhuan.hero.user.pojo.bo.UserAttentionBO>  不会返回null
     * @author  hanjunjun  
     * @date 2024/7/1       
     **/
    public List<UserAttentionBO> queryserAttentions(Long fromUid, List<Long> toUids) {
        if (Objects.isNull(fromUid)) {
            //如果为空则不查
            return new ArrayList<>();
        }

        if (CollectionUtils.isEmpty(toUids)) {
            throw new BusinessException(ExceptionMsgEnum.PARAM_ERROR);
        }
        toUids = toUids.stream().distinct().collect(Collectors.toList());
        log.info("getAttentionRelationListScf fromUid={}, toUids={}", fromUid, JsonUtils.toJsonWithoutNull(toUids));
        Result<Map<Long, AttentionRelationVO>> result =
            attentionManageFacade.getAttentionRelationListScf(fromUid, toUids);
        log.info("getAttentionRelationListScf result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("queryserAttentions失败");
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.SOCIAL_SERVICE_NAME, "getAttentionRelationListScf调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        Map<Long, AttentionRelationVO> data = result.getData();
        if (MapUtils.isEmpty(data)) {
            return new ArrayList<>();
        }
        List<AttentionRelationVO> resultList = new ArrayList<>(data.values());

        return resultList.stream().map(this::convertUserAttentionBO).collect(Collectors.toList());
    }

    /**
     * 自己的好友也关注了ta   查询好友头像
     *
     * @param fromUid  自己
     * @param toUid  关注了谁
     * @return java.util.List<java.lang.String>
     * @author  hanjunjun
     * @date 2024/7/8
     **/
    public List<String> queryContactList(Long fromUid, Long toUid) {
        if (Objects.isNull(fromUid) || Objects.isNull(toUid)) {
            return new ArrayList<>();
        }
        log.info("queryContactList fromUid={} toUids={}", fromUid, toUid);

        //k:toUids v:用户头像
        Map<Long, List<String>> profilePhotoMap = this.batchQueryContactList(fromUid, Collections.singletonList(toUid));
        log.info("queryContactList profilePhotoMap{}", JsonUtils.toJsonWithoutNull(profilePhotoMap));

        if (MapUtils.isEmpty(profilePhotoMap)) {
            log.info("没有也关注ta的好友");
            return new ArrayList<>();
        }
        return profilePhotoMap.get(toUid);
    }

    /**
     * 批量查询 自己的好友也关注了ta
     *
     * @param fromUid
     * @param toUids
     * @return java.util.Map<java.lang.Long,java.util.List<java.lang.String>>
     * @author  hanjunjun
     * @date 2024/7/30
     **/
    public Map<Long, List<String>> batchQueryContactList(Long fromUid, List<Long> toUids) {
        if (CollectionUtils.isEmpty(toUids)) {
            return new HashMap<>();
        }

        log.info("getContactListScf fromUid={} toUids={}", fromUid, toUids);
        Result<Map<Long, List<String>>> result =
            attentionManageFacade.getContactListScf(fromUid, toUids.stream().distinct().collect(Collectors.toList()));
        log.info("getContactListScf result{}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.SOCIAL_SERVICE_NAME, "getContactListScf调用失败");
            throw new BusinessException(ExceptionMsgEnum.COMMON_ERROR);
        }

        //k:toUids v:用户头像
        return result.getData();
    }




    public Map<Long, List<RecycleSheetScfVO>> queryPriceSheet(List<Long> uidLists, Date start, Date end) {
        log.info("queryRecentPriceSheet uid={} start={}, end={}", uidLists, start, end);
        Result<List<RecycleSheetScfVO>> recentRecycleSheetListByTime =
            priceSheetManageFacade.getRecentRecycleSheetListByTime(uidLists, start, end);
        log.info("queryRecentPriceSheet result={}", JsonUtils.toJsonWithoutNull(recentRecycleSheetListByTime));
        List<RecycleSheetScfVO> priceSheetList = RpcUtil.extractResult(recentRecycleSheetListByTime, uidLists, start, end);
        if(CollectionUtils.isEmpty(priceSheetList)){
            return Maps.newHashMap();
        }
        return priceSheetList.stream().collect(Collectors.groupingBy(RecycleSheetScfVO::getAuthorId, Collectors.toList()));
    }

    /**
     * 用户注销删除报价单和内容
     *
     * @param uid
     * @return void
     * @author  hanjunjun
     * @date 2024/7/12
     **/
    public boolean cancelAccount(Long uid) {
        log.info("userLogoutDeleteContentAndRecycleTable uid={}", uid);
        Result<Boolean> result = postAuthorityFacade.userLogoutDeleteContentAndRecycleTable(uid);
        log.info("userLogoutDeleteContentAndRecycleTable result={}", JsonUtils.toJsonWithoutNull(result));
        return result.isSuccess();
    }


    private UserAttentionBO convertUserAttentionBO(AttentionRelationVO attentionRelationVO) {
        UserAttentionBO bo = new UserAttentionBO();
        bo.setToUid(attentionRelationVO.getToUid());
        bo.setAttentionRelationCode(attentionRelationVO.getAttentionRelationCode());
        bo.setAttentionRelationName(attentionRelationVO.getAttentionRelationDesc());
        bo.setAttentionDay(attentionRelationVO.getAttentionDays());
        return bo;
    }
}
